package com.qqtop10.manager.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtop10.manager.model.IndustryCategory;
import com.qqtop10.manager.model.PagingArgs;
import com.qqtop10.manager.model.Result;
import com.qqtop10.manager.service.IndustryCategoryService;
import com.qqtop10.manager.utils.RandomUtils;

/**
 * 
 * @author Jovi
 *
 */
@RequestMapping("/manager/icategory")
@Controller
public class IndustryCategoryController extends BaseController {
	@Autowired
	@Qualifier("industryCategoryService")
	private IndustryCategoryService industryCategoryService;

	@RequestMapping("all.do")
	@ResponseBody
	public Result all(IndustryCategory input) {
		try {
			PagingArgs pagingArgs = getPagingArgs();
			return page(industryCategoryService.count(input), industryCategoryService.all(input, pagingArgs));
		} catch (Exception e) {
			return excep(e);
		}
	}

	/**
	 * @param name
	 * @return
	 */
	@RequestMapping("add.do")
	@ResponseBody
	public Result add(String name) {
		IndustryCategory industryCategory = new IndustryCategory();
		industryCategory.setUuid(RandomUtils.makeUUID());
		industryCategory.setCreatetime(new Date());
		industryCategory.setCreator(super.getLogindUuid());
		industryCategory.setName(name);
		industryCategoryService.add(industryCategory);
		return ok("操作成功！");
	}

	@RequestMapping("delete.do")
	@ResponseBody
	public Result delete(int[] ids) {
		try {
			IndustryCategory input = new IndustryCategory();
			input.setMender(getLogindUuid());
			input.setUpdatetime(now());
			int count = 0;
			for (int i = 0; i < ids.length; i++) {
				input.setId(ids[i]);
				if (industryCategoryService.delete(input) < 1) {
					ids[count] = ids[i];
					++count;
				}
			}
			if (count == 0) {
				return ok("操作成功！");
			} else if (count < ids.length) {
				return ok("操作已完成。但有些数据没有删除成功。");
			} else {
				return err("全部操作失败！");
			}
		} catch (Exception e) {
			return excep(e);
		}
	}
}
